package com.equipment.system.web.activemq;

import com.alibaba.fastjson.JSON;
import com.equipment.system.entity.Equipment;
import com.equipment.system.entity.EquipmentInfo;
import com.equipment.system.entity.FaultInfo;
import com.equipment.system.service.EquipmentInfoService;
import com.equipment.system.service.EquipmentService;
import com.equipment.system.service.FaultInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.TextMessage;
import java.sql.Timestamp;
import java.util.Map;

/**
 * 消费者
 *
 * @author Administrator
 */
@Component
public class Consumer {


	private final Logger logger = LoggerFactory.getLogger(Consumer.class);


	@Autowired
	private EquipmentService equipmentService;

	@Autowired
	private EquipmentInfoService infoService;

	private FaultInfoService faultInfoService;

	// 使用JmsListener配置消费者监听的队列，其中text是接收到的消息
	@Async
	@JmsListener(destination = "equipment.queue")
	public void receiveQueue(final TextMessage message) throws JMSException {
		try {
			String json = null;
			json = message.getText();
			Equipment equipment = JSON.parseObject(json, Equipment.class);
			logger.info(equipment.toString());
			equipmentService.insert(equipment);
			//确认接收，并成功处理了消息
			message.acknowledge();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

	// 使用JmsListener配置消费者监听的队列，其中text是接收到的消息
	@Async
	@JmsListener(destination = "Running_State")
	public void runningInfo(final TextMessage message) throws JMSException {
		try {
			String json = null;
			json = message.getText();
			Map<String,Object> map = (Map)JSON.parse(json);
			logger.info(json);
			EquipmentInfo equipmentInfo = new EquipmentInfo();
			equipmentInfo.setCreated(new Timestamp(System.currentTimeMillis()));
			equipmentInfo.setRunCount((Integer) map.get("yxcs"));
			equipmentInfo.setEquipmentCode(map.get("jqbh").toString());
			equipmentInfo.setRunState((Integer)map.get("id"));
			equipmentInfo.setEquipmentIp(map.get("ip").toString());
			equipmentInfo.setRunTime(new Timestamp(System.currentTimeMillis()));
			equipmentInfo.setEndTime(new Timestamp(System.currentTimeMillis()));
			equipmentInfo.setStartTime(new Timestamp(System.currentTimeMillis()));
			infoService.insert(equipmentInfo);
			//确认接收，并成功处理了消息
			message.acknowledge();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}


	// 使用JmsListener配置消费者监听的队列，其中text是接收到的消息
	@Async
	@JmsListener(destination = "fault.queue")
	public void FaultInfo(final TextMessage message) throws JMSException {
		try {
			String json = null;
			json = message.getText();
			Map<String,Object> map = (Map)JSON.parse(json);
			FaultInfo faultInfo = new FaultInfo();
			faultInfo.setMachineCode(map.get("machineCode").toString());
			faultInfo.setFaultMessage(map.get("troulemsg").toString());
			faultInfo.setFaultType(map.get("trouletype").toString());
			faultInfo.setFaultTime(new Timestamp(System.currentTimeMillis()));
			logger.info(json);
			faultInfoService.insert(faultInfo);
			//确认接收，并成功处理了消息
			message.acknowledge();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}